# replication code
# Long-Term Policymaking and Politicians’ Beliefs about Voters: Evidence from a Three-Year Panel Study of Politicians
# Lior Sheffer, Jack Lucas, Peter Loewen
library(tidyverse)
library(nnet)
library(rio)

# (set your working directory to folder with data here)
# setwd("")

data <- rio::import("governance_long.dta")
wide <- rio::import("governance_wide.dta")

###############################################################################
# figure 1

data %>% filter(!is.na(theory)) %>%
  mutate(theory = theory * 10) %>%
  group_by(theory) %>% 
  summarise(n = n()) %>% mutate(percent = n / sum(n)) %>%
  ggplot(., aes(x=theory, y=percent)) + 
  geom_bar(stat="identity") + xlab("") + ylab("") + 
  scale_x_continuous(breaks=c(0:10), labels=c("0\nShort-Term\nFocus", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10\nLong-Term\nFocus")) + 
  scale_y_continuous(expand=c(0,0), labels=scales::percent_format(accuracy = 1L), breaks = seq(0,.25,.05), limits=c(0,0.2)) + 
  theme_minimal() + theme(panel.border = element_rect(fill=NA, size=0.5)) + 
  ggtitle("Some say that voters are impatient and think about the short term when they vote.\nOthers say that voters are able to ignore short-term distractions and focus on the long term.\nWhere would you position yourself in this debate?")

###############################################################################
# figure 2

data %>% group_by(year) %>% summarise(mean = mean(bridge_vote)) %>%
  ggplot(., aes(x=year, y=mean)) + 
  geom_bar(stat="identity") + geom_text(aes(label = scales::percent(mean, accuracy=1L)), vjust=-0.3) + 
  scale_y_continuous(limits=c(0,1), labels=scales::percent, expand=c(0,0)) + 
  theme_minimal() + theme(panel.border = element_rect(fill=NA, size=0.5)) + 
  xlab("") + ylab("Vote for Long-Term Solution")

###############################################################################
# models for table 3
# please note that for reasons of confidentiality, data for model 2 not provided

q.mod1 <- lm(bridge_vote ~ qtype, data=data)
# q.mod2 <- lm(bridge_vote ~ qtype + age + woman + education + ideology + mayor + factor(region), data=bridge)
q.mod3 <- lm(bridge_vote ~ qtype + factor(mun_identifier), data=data)
q.mod4 <- lm(bridge_vote ~ qtype + factor(ind_identifier), data=data)

###############################################################################
# models for table 4
# please note that for reasons of confidentiality, data for model 2 not provided

t.mod1 <- lm(bridge_vote ~ theory + qtype, data=data)
# t.mod2 <- lm(bridge_vote ~ theory + qtype + prox + scales::rescale(age) + woman + education + ideology + mayor, data=data)
t.mod3 <- lm(bridge_vote ~ theory + qtype + factor(mun_identifier), data=data)
t.mod4 <- lm(bridge_vote ~ qtype + factor(mun_identifier), data=data %>% filter(theory<0.5))
t.mod5 <- lm(bridge_vote ~ qtype + factor(mun_identifier), data=data %>% filter(theory>0.5))

# additional models in appendix
t.mod6 <- lm(bridge_vote ~ theory + qtype + bridge_treat, data=data)
t.mod7 <- lm(bridge_vote ~ theory + qtype + bridge_treat + factor(mun_identifier), data=data)

###############################################################################
# figure 3

means.dat <- wide %>%
  mutate(theory = car::recode(theory, "0:0.4=0; 0.5=NA; 0.6:1=2"),
         change = car::recode(change, "'1 0' = '0 1'")) %>%
  filter(!is.na(theory))
means <- means.dat %>% filter(!is.na(theory)) %>% ungroup() %>% 
  group_by(theory, change) %>% 
  summarise(n = n()) %>% mutate(percent = n / sum(n))
means$change <- factor(means$change, labels=c("Always Select\nShort Term Policy",
                                              "Change Selection\nAcross Vignettes",
                                              "Always Select\nLong Term Policy"))
means$theory <- factor(means$theory, labels=c("Belief: Voters Have Short-Term Focus", "Belief: Voters Have Long-Term Focus"))
ggplot(means, aes(x=change, y=percent, group=theory, fill=theory)) + 
  geom_bar(stat="identity", position=position_dodge()) + coord_flip() + 
  geom_text(aes(label=scales::percent(percent, accuracy=1L)), hjust=-0.2, position = position_dodge(width=1)) + 
  xlab("") + ylab("") + 
  scale_y_continuous(labels=scales::percent, expand=c(0,0), limits=c(0,0.8)) + 
  theme_minimal() + theme(panel.border = element_rect(fill=NA, size=0.5),
                          legend.position = "bottom",
                          legend.title = element_blank(),
                          strip.text = element_text(hjust = 0),
                          panel.spacing = unit(2, "lines")) + 
  guides(fill = guide_legend(nrow=2)) + 
  scale_fill_manual(values=c("#7570b3", "#1b9e77"))

###############################################################################
# appendix: logit model
# please note that for reasons of confidentiality, data for model 2 not provided

logit.t.mod1 <- glm(bridge_vote ~ theory + qtype, data=data, family=binomial(link="logit"))
# logit.t.mod2 <- glm(bridge_vote ~ theory + qtype + prox + scales::rescale(age) + woman + education + ideology + mayor, data=data, family=binomial(link="logit"))
logit.t.mod3 <- glm(bridge_vote ~ theory + qtype + factor(mun_identifier), data=data, family=binomial(link="logit"))
logit.t.mod4 <- glm(bridge_vote ~ qtype + factor(mun_identifier), data=data %>% filter(theory<0.5), family=binomial(link="logit"))
logit.t.mod5 <- glm(bridge_vote ~ qtype + factor(mun_identifier), data=data %>% filter(theory>0.5), family=binomial(link="logit"))

###############################################################################
# appendix: question type across years

data$factorType <- car::recode(data$year, "2020=0; 2021=1; 2022=2")
q.mod5 <- lm(bridge_vote ~ factor(factorType) + factor(ind_identifier), data=data)

###############################################################################
# appendix: multinomial logit

wide <- wide %>% 
  mutate(choice = car::recode(change, "'0 0' = 'Always Short Term'; '0 1' = 'Change'; '1 0' = 'Change'; '1 1' = 'Always Long Term'"))
wide$choice <- factor(wide$choice, levels=c("Change","Always Short Term","Always Long Term"))
mlogit <- multinom(choice ~ theory, data=wide)

###############################################################################
# appendix: proximity model
# please note that for reasons of confidentiality, data for model 2 not provided

prox1 <- lm(bridge_vote ~ prox, data=data)
# prox2 <- lm(bridge_vote ~ prox + theory + qtype + scales::rescale(age) + woman + education + ideology, data=bridge)
prox3 <- lm(bridge_vote ~ prox + factor(ind_id), data=bridge)

###############################################################################
# appendix: issue importance/position
# please note that for reasons of confidentiality, data for model 2 not provided

# issue importance models
issnb0 <- lm(bridge_vote ~ theory + qtype, data = bridge %>% filter(iss_nb==0))
issnb1 <- lm(bridge_vote ~ theory + qtype, data = bridge %>% filter(iss_nb==1))
issnb2 <- lm(bridge_vote ~ theory + qtype + iss_nb, data=bridge)

# issue position models
b.wide <- wide %>%
  mutate(earlier_choice = as.numeric(substr(change,1,2)),
         choice_22 = as.numeric(substr(change,3,3)))
widemod1 <- lm(choice_22 ~ theory + earlier_choice, data=b.wide)
widemod2 <- lm(choice_22 ~ theory + earlier_choice + factor(group), data=b.wide)




